/**
 * 将列表数据处理为树形结构 XXX.children  模式
 *  */
export function generateTreeData(listData:[] , keyName:string , parentKeyName:string){
    let sourceListData = listData;//JSON.parse(JSON.stringify(listData));
    let targetTreeData = [];

    while(sourceListData.length > 0){
        var item = sourceListData[0];
        generateTreeNode(sourceListData , targetTreeData , item , keyName , parentKeyName);
    }
    return targetTreeData;
}

/**
 * 遍历树，删除一个指定的节点
 */
export function deleteTreeNode(treeData:[] , key:string , value:string){
    for( var i = 0 ; i < treeData.length ; i++ ){
        if(treeData[i][key] == value){
            treeData.splice(i , 1);
            return true;
        }else if( treeData[i].children != null && treeData[i].children.length > 0 ){
            let isSuccess = deleteTreeNode(treeData[i].children , key , value);
            if(isSuccess){
                return isSuccess;
            }
        }
    }

    return false;
}

/**
 * 生成树的一个节点
 *  */ 
export function generateTreeNode(sourceListData:[] , targetTreeData:[] , currentItem:any , keyName:string , parentKeyName:string){
    
    var parentKeyValue = currentItem[parentKeyName];

    var lobjCurrent = findNode(sourceListData , keyName , currentItem[keyName]);
    //if(lobjCurrent != null){
    //    sourceListData.splice(lobjCurrent.idx , 1);
    //    return;
    //}

    if(parentKeyValue == null || parentKeyValue == ""){
        targetTreeData.push(currentItem);
        sourceListData.splice(lobjCurrent.idx , 1);
        return;
    }

    // 在已插入数据中找父节点，如未找到，则不插入
    var lobj_parent = findNode(targetTreeData , keyName , parentKeyValue);
    if(lobj_parent != null){
        if(lobj_parent.data.children == null){
            lobj_parent.data.children = [];
        }
        lobj_parent.data.children.push(currentItem);

        sourceListData.splice(lobjCurrent.idx , 1);
        return;
    }

    // 从未插入数据中找父节点
    lobj_parent = findNode(sourceListData , keyName , parentKeyValue);
    if(lobj_parent != null){
        // 先完成父节点的插入
        generateTreeNode(sourceListData , targetTreeData , lobj_parent.data , keyName , parentKeyName);

        // 将本节点推到父节点下级
        if(lobj_parent.data.children == null){
            lobj_parent.data.children = [];
        }
        lobj_parent.data.children.push(currentItem);
        sourceListData.splice(lobjCurrent.idx , 1);
        return;
    }

    sourceListData.splice(lobjCurrent.idx , 1);
}

/**
 * 根据Key查找指定节点
 *  */ 
export function findNode(targetTreeData:[] , keyName:string , keyValue:string){
    
    for( var i = 0 ; i < targetTreeData.length ; i++ ){
        if( targetTreeData[i][keyName] == keyValue ){
            return {
                idx:i,
                data:targetTreeData[i]
            };
        }else if( targetTreeData[i].children != null ){
            // 在下级寻找
            var findResult = findNode(targetTreeData[i].children , keyName , keyValue );
            if( findResult != null ){
                return findResult;
            }
        }
    }
    return null;

}
